package com.etc.service.impl;

import com.etc.dao.*;
import com.etc.entity.*;
import com.etc.service.AdminRecordService;
import com.etc.util.EntityUtils;
import com.etc.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AdminRecordServiceImpl implements AdminRecordService {
    @Autowired
    private AdminRecordDao adminRecordDao;
    @Autowired
    private UserRecordDao userRecordDao;
    @Autowired
    private UnpaidDao unpaidDao;
    @Autowired
    private RecorddetailsDao recorddetailsDao;
    @Autowired
    private RecordDao recordDao;

    @Override
    public PageData<UserRecordFinall> FindRecordForAdmin(int page, int limit, String username) {

        int start = (page - 1) * limit;
        int count = recordDao.getCount("%" + username + "%");


        String remark = "已支付订单";
        Date movieDate;
        List<UserRecordFinall> userRecordFinalls = new ArrayList<UserRecordFinall>();

        //查询未支付的订单
        List<Unpaid> unpaids = unpaidDao.findAll();
        AdminRecord adminRecord = new AdminRecord();

        //通过工具类把查询结果映射到实体类上
        List<AdminRecord> adminRecords = EntityUtils.castEntity(adminRecordDao.FindAllRecordByAdmin(start,limit,username), AdminRecord.class, adminRecord);
        //通过循环把最终结果整合成一个list集合并返回
        for (int index = 0; index < adminRecords.size(); index++) {
            //获取毫秒差
            for (int i = 0; i < unpaids.size(); i++) {

                if (unpaids.get(i).getRid() == adminRecords.get(index).getRid()) {

                    if (unpaids.get(i).getPlacetime().getTime() + 10 * 60 * 1000 - new Date().getTime() < 0) {
                        ;
                        remark = "超时未支付订单";
                        break;

                    } else {

                        remark = "预处理订单";
                        break;
                    }

                } else {
                    remark = "已支付订单";

                }
            }
            UserRecordFinall userRecordFinall = new UserRecordFinall(adminRecords.get(index), userRecordDao.FindSeatsByRid(adminRecords.get(index).getRid()), remark);
            userRecordFinalls.add(userRecordFinall);
        }
        PageData<UserRecordFinall> pageData = new PageData<UserRecordFinall>(userRecordFinalls, count, limit, page);
        return pageData;
    }
}
